<template>
  <a-form
    id="formLogin"
    class="user-layout-login"
    ref="formLogin"
    :form="form"
    @submit="handleSubmit"
  >
    <a-form-item>
      <a-input
        size="large"
        type="text"
        placeholder="手机号 / 13000001111"
        v-decorator="[
          'username',
          {rules: [{ required: true, message: '请输入手机号' }], validateTrigger: 'change', initialValue: '13000001111'}
        ]"
      >
        <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
      </a-input>
    </a-form-item>

    <a-form-item>
      <a-input-password
        size="large"
        type="password"
        autocomplete="false"
        placeholder="密码 / 123456"
        v-decorator="[
          'password',
          {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur',initialValue: '123456'}
        ]"
      >
        <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
      </a-input-password>
    </a-form-item>
    <a-form-item>
      <a-row :span="24">
        <a-col :span="16">
          <a-input
            size="large"
            type="text"
            placeholder="验证码"
            autocomplete="false"
            v-decorator="[
              'code',
              {rules: [{ required: true, message: '请输入验证码' }]}
            ]"
          >
            <a-icon slot="prefix" type="safety-certificate" :style="{ color: 'rgba(0,0,0,.25)' }"/>
          </a-input>
        </a-col>
        <a-col :span="8">
          <div class="login-code">
            <span
              class="login-code-img"
              @click="refreshCode"
              v-if="code.type == 'text'">{{ code.value }}</span>
            <img
              id="login-code-img"
              class="login-code-img"
              @click="refreshCode"
              v-else/>
          </div>
        </a-col>
      </a-row>
    </a-form-item>
    <a-form-item style="margin-top:24px">
      <a-button
        size="large"
        type="primary"
        htmlType="submit"
        class="login-button"
        :loading="state.loginBtn"
        :disabled="state.loginBtn"
      >确定</a-button>
    </a-form-item>
  </a-form>
</template>

<script>
import { timeFix, randomLenNum, handleCodeImage } from '@/utils/util'
import { mapActions } from 'vuex'
export default {
  name: 'UserLogin',
  data () {
    return {
      form: this.$form.createForm(this),
      state: {
        loginBtn: false
      },
      code: {
        type: 'image',
        value: '',
        len: 4
      },
      loginForm: {
        randomStr: ''
      }
    }
  },
  methods: {
    ...mapActions(['Login', 'Logout']),
    handleSubmit (e) {
      e.preventDefault()
      const {
        form: { validateFields },
        state,
        Login
      } = this

      validateFields((err, values) => {
        if (!err) {
          values.type = 'user'
          values.uuid = this.loginForm.randomStr
          const loginParams = { ...values }
          Login(loginParams)
            .then((res) => this.loginSuccess(res))
            .catch(err => this.requestFailed(err))
            .finally(() => {
              state.loginBtn = false
            })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    refreshCode () {
      this.loginForm.randomStr = randomLenNum(this.code.len, true)
      this.code.type === 'text'
        ? (this.code.value = randomLenNum(this.code.len))
        : handleCodeImage(this.loginForm.randomStr, 'login-code-img')
    },
    loginSuccess (res) {
      this.$router.push({ name: 'index' })
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`
        })
      }, 1000)
    },
    requestFailed (err) {
      this.refreshCode()
      this.$notification['error']({
        message: '错误',
        description: ((err.response || {}).data || {}).msg || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  },
  created () {
    this.refreshCode()
  }
}
</script>

<style scoped lang="less">
  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }
  .login-code-img {
    /*margin-top: 2px;*/
    width: 100px;
    height: 38px;
    background-color: #fdfdfd;
    border: 1px solid #f0f0f0;
    color: #333;
    font-size: 14px;
    font-weight: bold;
    letter-spacing: 5px;
    line-height: 38px;
    text-indent: 5px;
    text-align: center;
  }
</style>
